package com.quectel.sdk.quecentrance;

import cn.hutool.crypto.digest.MD5;
import com.fasterxml.jackson.core.type.TypeReference;
import com.quectel.util.common.JacksonUtils;
import com.quectel.util.http.HttpRestUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 在这里编写说明
 *
 * @author: rananxin
 * @email: ryan.ran@quectel.com
 * @date: 2022-11-08 13:30:00
 */
public class QuecEntranceRequester {


    private static final Logger logger = LoggerFactory.getLogger(QuecEntranceRequester.class);

    private static final HttpRestUtils HTTP_REST_UTILS = HttpRestUtils.getTrustSSLInstance();

    private String appKey;
    private String appSecret;
    private String baseUrl;

    private QuecEntranceRequester(String baseUrl, String appKey, String appSecret) {
        this.baseUrl = baseUrl;
        this.appKey = appKey;
        this.appSecret = appSecret;
    }


    public static QuecEntranceRequester getInstance(String baseUrl, String appKey, String appSecret) {

        return new QuecEntranceRequester(baseUrl, appKey, appSecret);
    }

    public String get(String path, Map<String, Object> params) {
        List<NameValuePair> paramList = new ArrayList<>();
        if (params != null) {
            Set<Map.Entry<String, Object>> entries = params.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                paramList.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
            }
        }
        Map<String, String> header = genHeader();
        return HTTP_REST_UTILS.get(baseUrl + path, paramList, header, new TypeReference<String>() {
        }).getRight();
    }

    public String post(String path, Object params) {
        String body = null;
        if (params != null) {
            body = params instanceof String ? params.toString() : JacksonUtils.toJsonString(params);
        }

        Map<String, String> header = genHeader();
        return HTTP_REST_UTILS.post(baseUrl + path, body, header, new TypeReference<String>() {
        }).getRight();
    }

    public String delete(String path, Object params) {
        String body = null;
        if (params != null) {
            body = params instanceof String ? params.toString() : JacksonUtils.toJsonString(params);
        }

        Map<String, String> header = genHeader();
        return HTTP_REST_UTILS.delete(baseUrl + path, body, header, new TypeReference<String>() {
        }).getRight();
    }

    public String genUrl(String path) {
        return baseUrl + path;
    }

    public Map<String, String> genHeader() {
        long l = System.currentTimeMillis();
        String sign = MD5.create().digestHex(l + "#" + appSecret);
        Map<String, String > header = new HashMap<>();
        header.put("appKey", appKey);
        header.put("sign", sign);
        header.put("timestamp", l + "");
        return header;
    }

}
